package com.bra.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ZoomControls;

import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.BaiduMap.OnMarkerClickListener;
import com.baidu.mapapi.map.InfoWindow.OnInfoWindowClickListener;
import com.baidu.mapapi.map.BitmapDescriptor;
import com.baidu.mapapi.map.BitmapDescriptorFactory;
import com.baidu.mapapi.map.InfoWindow;
import com.baidu.mapapi.map.MapStatusUpdate;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.Marker;
import com.baidu.mapapi.map.MarkerOptions;
import com.baidu.mapapi.map.OverlayOptions;
import com.baidu.mapapi.model.LatLng;
import com.baidu.mapapi.model.LatLng;
import com.baidu.mapapi.search.core.SearchResult;
import com.baidu.mapapi.search.geocode.GeoCodeOption;
import com.baidu.mapapi.search.geocode.GeoCodeResult;
import com.baidu.mapapi.search.geocode.GeoCoder;
import com.baidu.mapapi.search.geocode.OnGetGeoCoderResultListener;
import com.baidu.mapapi.search.geocode.ReverseGeoCodeResult;
import com.bra.R;
import com.bra.ui.base.BaseActivity;

/**
 * 地图界面
 * 
 * @author lenovo
 *
 */
public class MapActivity extends BaseActivity implements
		OnGetGeoCoderResultListener {
	private MapView mMapView = null;
	private BaiduMap mBaiduMap;
	private BitmapDescriptor bitmap;
	private OverlayOptions option;
	private ImageView activity_back;
	private TextView activity_title;
	private GeoCoder mSearch = null; // 搜索模块，也可去掉地图模块独立使用
	private Bundle intent;
	private String info;
	private BitmapDescriptor marka = BitmapDescriptorFactory
			.fromResource(R.drawable.icon_marka);
	private InfoWindow mInfoWindow;
	private View view;
	private TextView map_click_text;
	private Marker mMarkerA;

	@Override
	protected View onCreateView(LayoutInflater inflater,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		return inflater.inflate(R.layout.activity_map, null);
	}

	@Override
	protected void onViewCreated(Bundle savedInstanceState) {
		intent = getIntent().getExtras();
		info = intent.getString("address");
		System.out.println(info + "=========================" + info);
		activity_title = (TextView) findViewById(R.id.activity_title);
		activity_back = (ImageView) findViewById(R.id.activity_back);
		activity_title.setText("地图");
		mMapView = (MapView) findViewById(R.id.bmapView);
		mMapView.showScaleControl(false);// 隐藏比例尺控件
		mMapView.showZoomControls(false);// 影藏放大缩小
		View child = mMapView.getChildAt(1);
		if (child != null
				&& (child instanceof ImageView || child instanceof ZoomControls)) {
			child.setVisibility(View.INVISIBLE);
		}
		mBaiduMap = mMapView.getMap();
		// 普通地图
		mBaiduMap.setMapType(BaiduMap.MAP_TYPE_NORMAL);
		MapStatusUpdate zoomTo = MapStatusUpdateFactory.zoomTo(16.0f);
		mBaiduMap.setMapStatus(zoomTo);
		// 初始化搜索模块，注册事件监听
		mSearch = GeoCoder.newInstance();
		mSearch.setOnGetGeoCodeResultListener(this);
		mSearch.geocode(new GeoCodeOption().city("北京").address(info));

		mBaiduMap.setOnMarkerClickListener(new OnMarkerClickListener() {

			@Override
			public boolean onMarkerClick(Marker mark) {
				System.out.println("===MARK的点击事件=========");
				view = View.inflate(MapActivity.this, R.layout.map_mark, null);
				map_click_text = (TextView) view
						.findViewById(R.id.map_click_text);
				map_click_text.setText(info);

				LatLng ll = mark.getPosition();
				mInfoWindow = new InfoWindow(view, ll, -47);
				mBaiduMap.showInfoWindow(mInfoWindow);

				view.setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View v) {
						mBaiduMap.hideInfoWindow();
					}
				});
				return true;
			}
		});
	}

	public View.OnClickListener click = new OnClickListener() {

		@Override
		public void onClick(View v) {
			switch (v.getId()) {
			case R.id.activity_back:
				finish();
				break;
			default:
				break;
			}

		}
	};

	@Override
	protected void onPause() {
		mMapView.onPause();
		super.onPause();
	}

	@Override
	protected void onResume() {
		mMapView.onResume();
		super.onResume();
	}

	@Override
	protected void onDestroy() {
		mMapView.onDestroy();
		mSearch.destroy();
		super.onDestroy();
		marka.recycle();
	}

	@Override
	protected void setListener() {
		activity_back.setOnClickListener(click);

	}

	@Override
	public void onGetGeoCodeResult(GeoCodeResult result) {
		if (result == null || result.error != SearchResult.ERRORNO.NO_ERROR) {
			Toast.makeText(MapActivity.this, "抱歉，未能找到结果", Toast.LENGTH_LONG)
					.show();
			return;
		}
		mBaiduMap.clear();
		mMarkerA = (Marker) mBaiduMap.addOverlay(new MarkerOptions().position(
				result.getLocation()).icon(marka));
		mBaiduMap.setMapStatus(MapStatusUpdateFactory.newLatLng(result
				.getLocation()));
		String strInfo = String.format("纬度：%f 经度：%f",
				result.getLocation().latitude, result.getLocation().longitude);
		Toast.makeText(MapActivity.this, strInfo, Toast.LENGTH_LONG).show();

	}

	@Override
	public void onGetReverseGeoCodeResult(ReverseGeoCodeResult arg0) {
		// TODO Auto-generated method stub

	}
}
